<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}我的博客{% endblock %}</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="{{ url_for('main.index') }}">我的博客</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <div class="navbar-nav me-auto">
                    <ul class="navbar-nav">
                        <li class="nav-item {% if request.endpoint == 'main.index' %}active{% endif %}">
                            <a class="nav-link" href="{{ url_for('main.index') }}">首页</a>
                        </li>
                    </ul>
                    <ul class="navbar-nav">
                        <li class="nav-item {% if request.endpoint and request.endpoint.startswith('blog.') %}active{% endif %}">
                            <a class="nav-link" href="{{ url_for('blog.blog_index') }}">博文</a>
                        </li>
                    </ul>
                    <ul class="navbar-nav">
                        <li class="nav-item {% if request.endpoint == 'main.about' %}active{% endif %}">
                            <a class="nav-link" href="{{ url_for('main.about') }}">关于</a>
                        </li>
                    </ul>
                </div>
                {% if session.get('user_id') %}
                    <ul class="navbar-nav">
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                {{ session.get('username') }}
                            </a>
                            <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <li><a class="dropdown-item" href="#">个人中心</a></li>
                                <li><hr class="dropdown-divider"></li>
                                <li><a class="dropdown-item" href="{{ url_for('auth.logout') }}">退出登录</a></li>
                            </ul>
                        </li>
                    </ul>
                {% else %}
                    <ul class="navbar-nav">
                        <li class="nav-item">
                            <a class="nav-link" href="{{ url_for('auth.login') }}">登录</a>
                        </li>
                    </ul>
                    <ul class="navbar-nav">
                        <li class="nav-item">
                            <a class="nav-link" href="{{ url_for('auth.register') }}">注册</a>
                        </li>
                    </ul>
                {% endif %}
            </div>
        </div>
    </nav>

    <div class="main-content container mt-4 mb-5">
        <!-- Flash消息显示 -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show auto-dismiss" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}
        
        {% block content %}{% endblock %}
    </div>

    <footer class="py-3 bg-light">
        <div class="container text-center">
            <p class="mb-0">© 2025 我的博客 - 使用Flask构建</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        // 5秒后自动关闭所有flash消息
        document.addEventListener('DOMContentLoaded', function() {
            setTimeout(function() {
                const alerts = document.querySelectorAll('.alert.auto-dismiss');
                alerts.forEach(alert => {
                    // 检查bootstrap是否可用，如果可用则使用其API关闭
                    if (typeof bootstrap !== 'undefined' && bootstrap.Alert) {
                        const bsAlert = bootstrap.Alert.getInstance(alert);
                        if (bsAlert) {
                            bsAlert.close();
                        } else {
                            // 如果没有实例，则创建一个新实例再关闭
                            new bootstrap.Alert(alert).close();
                        }
                    } else {
                        // 备选方案：直接添加关闭类
                        alert.classList.remove('show');
                        alert.classList.add('hide');
                        // 等待动画完成后移除元素
                        setTimeout(() => {
                            alert.remove();
                        }, 100);
                    }
                });
            }, 5000);
        });
    </script>
</body>

</html>